package com.pyip.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyip.reggie.Entity.Employee;
import com.pyip.reggie.common.R;
import com.pyip.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
       return employeeService.login(request,employee);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @RequestMapping
    public R<String> addNewEmployee(HttpServletRequest request, @RequestBody Employee employee){
        return employeeService.addNewEmployee(request,employee);
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public R<Page<Employee>> getEmployeePage(int page, int pageSize, String name){
        return employeeService.getEmployeePage(page,pageSize,name);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateEmployeeById(HttpServletRequest request,@RequestBody Employee employee){
        return employeeService.updateEmployeeById(request,employee);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public R<Employee> getEmployeeById(@PathVariable Long id){
        Employee ans = employeeService.getById(id);
        if(ans == null){
            return R.error("暂无信息");
        }
        return R.success(ans);
    }
}
